const { ethers } = require("hardhat");
const { task } = require("hardhat/config")

task("interact-fundme")
    .addParam("addr", "fundme contract address")
    .setAction(async (taskArgs, hre) => {
        const fundMeFactory = await ethers.getContractFactory("FundMe")

        const fundMe = await fundMeFactory.attach(taskArgs.addr)
        console.log("contract deployed")

        // init 2 accounts
        const [firstAccount, secondAccount] = await ethers.getSigners();
        console.log("first account address is ", firstAccount.address)
        console.log("second account address is ", secondAccount.address)

        // fund contract with first account
        const fundTx = await fundMe.fund({ value: ethers.parseEther("0.5") })
        await fundTx.wait()

        console.log(`2 accounts are ${firstAccount.address} and ${secondAccount.address}`)

        // check balance of contract
        const balanceOfContract = await ethers.provider.getBalance(fundMe.target)
        console.log(`Balance of the contract is ${balanceOfContract}`)

        // fund contract whith second
        const fundTxWithSecondAccount = await fundMe.connect(secondAccount).fund({ value: ethers.parseEther("0.5") })
        await fundTxWithSecondAccount.wait()

        // check balance of contract
        const balanceOfContractAfterSecondFund = await ethers.provider.getBalance(fundMe.target)
        console.log(`Balance of the contract is ${balanceOfContractAfterSecondFund}`)
        // check mapping fundersToAmount
        const firstAccountBalanceInFundMe = await fundMe.fundersToAmount(firstAccount.address)
        console.log(`firstToAmount is  ${firstAccountBalanceInFundMe} is ${firstAccount.address}`)
        const secondAccountBalanceInFundMe = await fundMe.fundersToAmount(secondAccount.address)
        console.log(`secondToAmount is ${secondAccountBalanceInFundMe} is ${secondAccount.address}`)


    })

module.exports = {};